<?php
/*
 * Created on May 15, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
?>
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 *  User_group Model Class
 *
 */
class  Slider_model extends CI_Model
{
	/**
	 * @var unknown_type
	 */
	var $id  ;
	/**
	 * @var unknown_type
	 */
	var $category_parent_id  ;
	/**
	 * @var unknown_type
	 */
	var $name_vi  ;
	/**
	 * @var unknown_type
	 */
	var $name_en  ;
	/**
	 * @var unknown_type
	 */
	var $description_vi;
	/**
	 * @var unknown_type
	 */
	var $description_en;
	/**
	 * @var unknown_type
	 */
	var $picture;
	/**
	 * @var unknown_type
	 */
	var $active;
	/*
	 * @access	public
	 */

	function __construct() {
		parent::__construct();
	}

	function create($category){

		$sql = "INSERT INTO `gtb_slider` " .
				"(`image`,
				`vi_title`,
				`en_title`,
				`vi_description`,
				`en_description`,
				`url`,
				`active`,
				`date_add`) " .
				"VALUES ";
		$sql .= "(" .
		$this->db->escape($category['image']).", " .
		$this->db->escape($category['vi_title']).", " .
		$this->db->escape($category['en_title']).", " .
		"'" . ($category['vi_description'])."', " .
		"'" . ($category['en_description'])."', " .
		$this->db->escape($category['url']).", " .	 
		intval($category['active']).", " .
		$this->db->escape($category['date_add']).
		");";

		$this->db->query($sql);
		
		$id = $this->db->insert_id();
		if ($id){
			$sql = "UPDATE gtb_slider SET position = $id WHERE id = $id";
			$this->db->query($sql);
		}
		return $id;
	}

	function update($category){
		$sql = "UPDATE gtb_slider
				SET 
				image = " .  $this->db->escape($category['image']) . ", " .
				"vi_title = " .  $this->db->escape($category['vi_title']) . "," .
				"en_title = " .  $this->db->escape($category['en_title']) . ", " .
				"vi_description = '" .  ($category['vi_description']) . "', " .
				"en_description = '" .  ($category['en_description']) . "', " .
				"active = " .  intval($category['active']) . ", " .
				"position = " .  intval($category['position']) . ", " .
				"url = " .  $this->db->escape($category['url']) . ", " .
				"date_upd = " .  $this->db->escape($category['date_upd']) .
				" WHERE id = " .intval($category['id']);
		$this->db->query($sql);
		echo $sql . '<br>';
		return $this->db->affected_rows();
	}

	function delete($id){
		if(!is_array($id)) $id = array($id);
		$ids = implode(',', $id);
		$sql = "DELETE FROM gtb_slider WHERE id IN ($ids)";
		//$sql = "DELETE FROM gtb_pcategory WHERE id_pcategory = " . intval($id);
		$this->db->query($sql);
		return $this->db->affected_rows();
	}

function getAllAdmin($offset, $limit, $id_parent_pcategory) {
        /* $where = ' WHERE ';
          foreach($parameter as $para_name => $value){
          $where .= ' ' . $para_name . '=' . $this->db->escape($value) . ',';
          }

          $where = substr($where, 0, strlen($where)-1);

          if(count($parameter) == 0) $where = '';
         */
        $sql = "SELECT a.*," .
                " (SELECT MAX(t3.order_view) FROM gtb_pcategory t3) as max_order,
					(SELECT MIN(t4.order_view) FROM gtb_pcategory t4) as min_order, 
					(SELECT (t5.id_pcategory) FROM gtb_pcategory t5 WHERE t5.order_view > a.order_view AND id_parent_pcategory = " . intval($id_parent_pcategory) . " LIMIT 0,1) as id_next,
					(SELECT (t6.id_pcategory) FROM gtb_pcategory t6 WHERE t6.order_view < a.order_view AND id_parent_pcategory = " . intval($id_parent_pcategory) . " ORDER BY t6.order_view DESC LIMIT 0,1) as id_preview " .
                " FROM gtb_pcategory a" .
                " WHERE id_parent_pcategory = " . intval($id_parent_pcategory) .
                " ORDER BY a.order_view DESC" .
                " LIMIT " . intval($offset) . ", " . intval($limit);

        //echo $sql;
        $query = $this->db->query($sql);
        $result = $query->result();
        $query->free_result();
        return $result;
    }
    
	function getAll($offset, $number_record, $active = -1, $admin = 1){
		$result = null;
		$sql = "";
		
		if ($admin){
			$sql = "SELECT a.*," .
                " (SELECT MAX(t3.position) FROM gtb_slider t3) as max_order,
					(SELECT MIN(t4.position) FROM gtb_slider t4) as min_order, 
					(SELECT (t5.id) FROM gtb_slider t5 WHERE t5.position > a.position AND (active = " . intval($active) . " OR -1 = " . intval($active) . ") LIMIT 0,1) as id_preview,
					(SELECT (t6.id) FROM gtb_slider t6 WHERE t6.position < a.position AND (active = " . intval($active) . " OR -1 = " . intval($active) . ") ORDER BY t6.position DESC LIMIT 0,1) as id_next" .
                " FROM gtb_slider a" .
                " WHERE active = ". intval($active) . " OR -1 = ". intval($active) .
                " ORDER BY a.position DESC" .
                " LIMIT " . intval($offset) . ", " . intval($number_record);
		}
		else
			$sql = "SELECT *  " .
				" FROM gtb_slider " .
				" WHERE active = ". intval($active) . " OR -1 = ". intval($active) .
				" ORDER BY position DESC".
				" LIMIT $offset, $number_record";
		//echo $sql;
		$query = $this->db->query($sql);
		$result = $query->result_array();

		return $result;
	}

	function countAll($active = -1){
		$result = null;
		$sql = "SELECT COUNT(*) as num_record" .
				" FROM gtb_slider " .
				" WHERE active = ". intval($active) . " OR -1 = ". intval($active) ;

		$query = $this->db->query($sql);
		$result = $query->result();

		return $result[0]->num_record;
	}

	function getAllByIds($ids){
		$ids = implode(',', $ids);
		$result = null;
		$sql = "SELECT n.* " .
				" FROM gtb_slider n " .
				" WHERE id IN ($ids) " .
				" ORDER BY n.id DESC";

		$query = $this->db->query($sql);
		$result = $query->result_array();

		return $result;
	}

	function getByParentCode($code_parent){
		$result = null;
		$sql = "SELECT *
				FROM gtb_pcategory WHERE code_parent_pcategory = '".$this->db->escape($code_parent)."' ORDER BY id_pcategory DESC";

		$query = $this->db->query($sql);
		$result = $query->result();

		return $result;
	}

	function getMaxPosition($category_parent_id){

		$sql = "SELECT MAX(position) as position_max
				FROM category 
				WHERE category_parent_id = $category_parent_id";
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0]->position_max;
		}
		 
		return 0;
	}

	function getById($id){

		$sql = "SELECT *
				FROM gtb_slider 
				WHERE id = " . intval($id);
		$query = $this->db->query($sql);
		if($result = $query->result_array()){
			return $result[0];
		}
		 
		return null;
	}

	function getByCode($code){

		$sql = "SELECT *
				FROM gtb_product 
				WHERE code = " . $this->db->escape($code);
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0];
		}
		 
		return null;
	}

	function getByFriendlyUrl($link_rewrite){

		$sql = "SELECT *
				FROM gtb_news 
				WHERE friendly_url = " . $this->db->escape($link_rewrite);
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0];
		}
		 
		return null;
	}

	function getByFriendlyUrl2($link_rewrite){
		//$sql = "UPDATE gtb_news set friendly_url = CONCAT (id_news, '')";
		//$this->db->query($sql);

		$sql = "SELECT *
				FROM gtb_news 
				WHERE friendly_url = " . $this->db->escape($link_rewrite);
		$query = $this->db->query($sql);
		if($result = $query->result_array()){
			return $result[0];
		}
		 
		return null;
	}

	function isInGroup($user_id, $group_id){
		$sql = "SELECT 1 as is_member FROM user_group ug
				INNER JOIN users u ON u.id = ug.user_id
				WHERE group_id = $group_id AND ug.user_id = $user_id";
		$query = $this->db->query($sql);
		if($query->result())
		return 1;

		return 0;

	}

	function setImage($image, $newsID){
		$sql = "UPDATE gtb_news SET image = ". $this->db->escape($image).
                   " WHERE id_news = " . intval($newsID);
		$query = $this->db->query($sql);
		return $this->db->affected_rows();
	}

	// for home page:
	function getAllByCategoryIds($ids, $limit = 5){
		$ids = implode(',', $ids);
		$result = array();
		$sql = "SELECT n.* " .
				" FROM gtb_news n " .
				" WHERE n.id_ncategory IN ($ids) AND active = 1" .
				" ORDER BY id_news DESC" . 
				" LIMIT 0, " . intval($limit);

		$query = $this->db->query($sql);
		$result = $query->result_array();

		return $result;
	}

	function getNewsOther($news_id, $cat_id, $limit = 10){
		$result = array();
		$sql = "SELECT *
				FROM gtb_news 
				WHERE active = 1 AND id_news < " . intval($news_id) . " AND id_ncategory = " . intval($cat_id).
				" LIMIT 0, " . intval($limit);
		$query = $this->db->query($sql);

		if($result = $query->result_array()){
			//print_r($result);
			return $result;
		}
		 
		return $result;
	}

	function search($keyword, $offset = 0, $limit = 20){
		$result = array();

		// create fulltext for multy column if search on multy colmn.
		$sql = "SELECT *, MATCH(vi_name, vi_description) AGAINST (".$this->db->escape($keyword).") AS score
				FROM gtb_news 
				WHERE MATCH (vi_name, vi_description) AGAINST (".$this->db->escape($keyword).")
				ORDER BY id_news DESC";
			
		$query = $this->db->query($sql);

		if($result = $query->result_array()){
			//print_r($result);
			return $result;
		}
		 
		return $result;
	}

	function countSearch($keyword, $offset = 0, $limit = 20){

		// create fulltext for multy column if search on multy colmn.
		$sql = "SELECT count(*) as number_record
				FROM gtb_news 
				WHERE MATCH (vi_name, vi_description) AGAINST (".$this->db->escape($keyword).")
				";
			
		$query = $this->db->query($sql);

		if($result = $query->result_array()){
			//print_r($result);
			return $result[0]['number_record'];
		}
		 
		return 0;
	}

	//
}

// EOF

